﻿using UnityEngine;
using System.Collections;


using XInputDotNetPure; // Required in C#


public class PlayerCursor : MonoBehaviour {


	bool playerIndexSet = false;
	PlayerIndex playerIndex;
	GamePadState state;
	GamePadState prevState;


	bool		UseLeftThumbStick;
	Vector2 	ThumbstickVecRight;
	Vector2		ThumbstickVecLeft;


	public float min = -10;
	public float max = 10;
	public float controllerThreshhold = 0.2f;
	public float speed = 4;


	public enum AXIS
	{
		Y,
		X
	}

	public AXIS axis = AXIS.X;

	public enum DIRECTION
	{
		UP,
		DOWN,
		LEFT,
		RIGHT
	}

	public DIRECTION dir = DIRECTION.UP;


	void FindPlayerIndex()
	{
		// Find a PlayerIndex, for a single player game
		// Will find the first controller that is connected ans use it
		if (!playerIndexSet || !prevState.IsConnected)
		{
			for (int i = 0; i < 4; ++i)
			{
				PlayerIndex testPlayerIndex = (PlayerIndex)i;
				GamePadState testState = GamePad.GetState(testPlayerIndex);
				if (testState.IsConnected)
				{
					Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
					playerIndex = testPlayerIndex;
					playerIndexSet = true;
				}
			}
		}
		
		prevState = state;
		state = GamePad.GetState(playerIndex);
		
	}


	// Use this for initialization
	void Start () 
	{




	
	}
	
	// Update is called once per frame
	void Update () 
	{
		UpdateXboxController();


		if(Mathf.Abs(GetControllerDirectionInput()) > controllerThreshhold)
		{
			SetCurrentPosition(GetCurrentPosition() + (GetControllerDirectionInput() * Time.deltaTime * speed));
		}

	}

	public float GetCurrentPosition()
	{
		if(axis == AXIS.X)
		{
			return transform.position.x;
		}
		else
		{
			return transform.position.y;
		}
	}

	void SetCurrentPosition(float pos)
	{
		if(axis == AXIS.X)
		{
			Vector3 temp =  transform.position;
			temp.x = pos;

			if(temp.x < min)
			{
				temp.x = min;
			}
			else if(temp.x > max)
			{
				temp.x = max;
			}

			transform.position = temp; 
		}
		else
		{
			Vector3 temp =  transform.position;
			temp.y = pos;

			if(temp.y < min)
			{
				temp.y = min;
			}
			else if(temp.y > max)
			{
				temp.y = max;
			}

			transform.position = temp; 
		}
	}

	public float GetControllerDirectionInput()
	{
		if(axis == AXIS.X)
		{
			return ThumbstickVecLeft.x;
		}
		else
		{
			return ThumbstickVecRight.y;
		}
	}

	public float GetIntensity()
	{
		if(axis == AXIS.X)
		{
			if(Mathf.Abs(ThumbstickVecLeft.y) < controllerThreshhold)
			{
				return 0;
			}
			return ThumbstickVecLeft.y;
		}
		else
		{
			if(Mathf.Abs(ThumbstickVecRight.x) < controllerThreshhold)
			{
				return 0;
			}
			return ThumbstickVecRight.x;
		}
	}

	public float GetIntensityInDirection()
	{
		if(axis == AXIS.X)
		{
			if(dir == DIRECTION.DOWN)
			{
				if(GetIntensity() < 0)
				{
					return Mathf.Abs(GetIntensity());
				}
			}
			else if(dir == DIRECTION.UP)
			{
				if(GetIntensity() > 0)
				{
					return GetIntensity();
				}
			}

		}
		else
		{
			if(dir == DIRECTION.LEFT)
			{
				if(GetIntensity() < 0)
				{
					return Mathf.Abs(GetIntensity());
				}
			}
			else if(dir == DIRECTION.RIGHT)
			{
				if(GetIntensity() > 0)
				{
					return GetIntensity();
				}
			}
		}

		return 0;
	}

	void UpdateXboxController()
	{
		prevState = state;
		state = GamePad.GetState(playerIndex);

		ThumbstickVecLeft.x = state.ThumbSticks.Left.X;
		ThumbstickVecLeft.y = state.ThumbSticks.Left.Y;

		ThumbstickVecRight.x = state.ThumbSticks.Right.X;
		ThumbstickVecRight.y = state.ThumbSticks.Right.Y;


	}

	public float GetDistanceFromCursor(Vector3 vec)
	{
		if(axis == AXIS.X)
		{
			return Mathf.Abs(vec.x - transform.position.x);
		}
		else
		{
			return Mathf.Abs(vec.y - transform.position.y);
		}

	}

	public Vector3 GetDirection()
	{
		if(axis == AXIS.X)
		{
			if(Mathf.Abs(ThumbstickVecLeft.y) < controllerThreshhold)
			{
				return new Vector3();
			}
			return new Vector3(0, ThumbstickVecLeft.y, 0);
		}
		else
		{
			if(Mathf.Abs(ThumbstickVecRight.x) < controllerThreshhold)
			{
				return new Vector3();
			}
			return new Vector3(ThumbstickVecRight.x, 0, 0);
		}
	}


}
